package org.jeecg.modules.sg.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.sg.entity.*;
import org.jeecg.modules.sg.mapper.SgInRoomMapper;
import org.jeecg.modules.sg.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description: 入宿管理
 * @Author: jeecg-boot
 * @Date:   2022-09-27
 * @Version: V1.0
 */
@Service
public class SgInRoomServiceImpl extends ServiceImpl<SgInRoomMapper, SgInRoom> implements ISgInRoomService {
    @Autowired
    public ISgPersonService iSgPersonService;
    @Autowired
    public ISgBerthService iSgBerthService;
    @Autowired
    public ISgRoomService iSgRoomService;
    @Autowired
    public IVSgRoomTrendsService iVSgRoomTrendsService;

    public void savePerson(SgInRoom sgInRoom, SgPerson sgPerson){
        BeanUtils.copyProperties(sgInRoom,sgPerson);
        sgPerson.setId(sgInRoom.getPresonId());
        sgPerson.setStatus("yfp");
        iSgPersonService.save(sgPerson);
    }

    /**
     * 入宿单核心逻辑，如果当前入职单则先把入宿单当做人员id占用房间床位
     * @param sgInRoom
     */
    @Override
    @Transactional
    public void saveSgInRoom(SgInRoom sgInRoom) {
        if(StringUtils.isNotBlank(sgInRoom.getBerthId())){
            //新增人员表数据
            SgPerson sgPerson=new SgPerson();
            savePerson(sgInRoom,sgPerson);
            //预占用床位
            SgBerth sgBerth=iSgBerthService.getById(sgInRoom.getBerthId());
            sgBerth.setPersonId(sgPerson.getId());
            iSgBerthService.updateById(sgBerth);
            sgInRoom.setPresonId(sgPerson.getId());
            sgInRoom.setStatus("yfp");
        }else{
            sgInRoom.setStatus("dfp");
        }
        this.save(sgInRoom);
    }

    @Override
    public void deleteSgInRoom(String id) {
        SgInRoom sgInRoom=this.getById(id);
        iSgPersonService.removeById(sgInRoom.getPresonId());
        this.removeById(id);
    }

    @Override
    public void copyOne(String id) {
        SgInRoom sgInRoom=this.getById(id);
        sgInRoom.setId(null);
        sgInRoom.setStatus("xj");
        sgInRoom.setPresonId(null);
        sgInRoom.setIdcard(null);
        sgInRoom.setRoomId(null);
        sgInRoom.setBerthId(null);
        this.save(sgInRoom);
    }

    @Override
    public void deleteSgInRooms(List<String> ids) {
        for (String id:ids){
            SgInRoom sgInRoom=this.getById(id);
            iSgPersonService.removeById(sgInRoom.getPresonId());
            this.removeById(id);
        }
    }

    @Override
    @Transactional
    public void audSgInRooms(String[] ids) {
        for (String id:ids){
            SgInRoom sgInRoom=this.getById(id);
            if("ysh".equals(sgInRoom.getStatus())){
            }else{
                SgPerson sgPerson=new SgPerson();
                savePerson(sgInRoom,sgPerson);
                //修改人员状态
                sgPerson.setStatus("rs");//入宿
                //放入第一次入住的时间
                sgPerson.setInitInRoomDate(sgPerson.getInRoomDate());
                iSgPersonService.updateById(sgPerson);
                //修改床位的状态
                SgBerth sgBerth=iSgBerthService.getById(sgInRoom.getBerthId());
                sgBerth.setPersonId(sgPerson.getId());
                iSgBerthService.updateById(sgBerth);
                //修改入宿单
                sgInRoom.setStatus("ysh");
                sgInRoom.setAudDate(new Date());//审核时间
                sgInRoom.setPresonId(sgPerson.getId());
                this.updateById(sgInRoom);
            }
        }
    }

    @Override
    @Transactional
    public void yjfpSgInRoom(String[] ids) {
        //查询当前待分配的记录
        List<SgInRoom> sgInRoomList=this.lambdaQuery().eq(SgInRoom::getStatus,"dfp").list();
        for(SgInRoom sgInRoom:sgInRoomList){
            //根据劳务公司，分配
            List<VSgRoomTrends> vSgRoomTrendsList= iVSgRoomTrendsService.lambdaQuery()
                    .eq(VSgRoomTrends::getRoomCompanyId,sgInRoom.getCompanyId())
                    .eq(VSgRoomTrends::getRoomSex,sgInRoom.getSex())
                    .isNull(VSgRoomTrends::getPersonId)
                    .list();
            //如果公司房间不为空，则进行分配
            if(vSgRoomTrendsList.size()>0){
                sgInRoom.setRoomId(vSgRoomTrendsList.get(0).getRoomId());
                sgInRoom.setBerthId(vSgRoomTrendsList.get(0).getId());
                sgInRoom.setStatus("yfp");
                //新增人员表数据
                SgPerson sgPerson=new SgPerson();
                savePerson(sgInRoom,sgPerson);
                //修改床位的状态
                SgBerth sgBerth=iSgBerthService.getById(sgInRoom.getBerthId());
                sgBerth.setPersonId(sgInRoom.getId());
                iSgBerthService.updateById(sgBerth);
                sgInRoom.setPresonId(sgPerson.getId());
                this.updateById(sgInRoom);
            }
        }
    }

    @Override
    @Transactional
    public void updateSgInRoom(SgInRoom sgInRoom) {
        //取出旧的人员数据
        SgInRoom oldSgInRoom=this.getById(sgInRoom.getId());
        if(StringUtils.isNotBlank(oldSgInRoom.getBerthId())){
            //删除当前的占用的房间
            SgBerth oldSgBerth=iSgBerthService.getById(oldSgInRoom.getBerthId());
            oldSgBerth.setPersonId(null);
            iSgBerthService.updateById(oldSgBerth);
        }
        if(StringUtils.isNotBlank(sgInRoom.getBerthId())){
            //判断当前是否有人员id
            if(StringUtils.isNotBlank(sgInRoom.getPresonId())){
                sgInRoom.setStatus("yfp");
                //预占用床位
                SgBerth sgBerth=iSgBerthService.getById(sgInRoom.getBerthId());
                sgBerth.setPersonId(sgInRoom.getPresonId());
                iSgBerthService.updateById(sgBerth);
            }else{
                //新增人员表数据
                SgPerson sgPerson=new SgPerson();
                savePerson(sgInRoom,sgPerson);
                SgBerth sgBerth=iSgBerthService.getById(sgInRoom.getBerthId());
                sgBerth.setPersonId(sgPerson.getId());
                iSgBerthService.updateById(sgBerth);
            }
        }else{
            //修改入宿单据
            sgInRoom.setStatus("dfp");
        }
        this.updateById(sgInRoom);
    }
}
